package org.liang.auth.service.impl;

import org.liang.auth.service.IPermissionScanService;
import org.liang.auth.service.IPermissionService;
import org.liang.base.annotation.RonghuaPermission;
import org.liang.base.enums.RequestTypeEnum;
import org.liang.base.utils.ClassUtils;
import org.liang.auth.domain.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Objects;

@Service
public class PermissionScanServiceImpl implements IPermissionScanService {

    @Value("${scan.package-name}")
    private String packages;

    @Autowired
    private IPermissionService permissionService;

    /*
     * name----类或方法上name
     *url----类或方法上xxxMapping(判断)
     *desc----类或方法上desc
     *sn------xxxController:方法
     * parent_id------parent
     * */

    @Override
    public void scan() {

        //清空原表中数据
        permissionService.deleteAll();

        //获取带有权限注解的所有接口信息(Controller)
        List<Class> allClassName = ClassUtils.getAllClassName(packages);

        for (Class clazz : allClassName){
            //获取权限注解接口
            RonghuaPermission annotation = (RonghuaPermission)clazz.getAnnotation(RonghuaPermission.class);
            //判断接口上权限注解是否为空
            if(Objects.isNull(annotation)){
                //是----说明没有权限
                continue;
            }

            //获取 RequestMapping 上的路径
            RequestMapping annotation2 = (RequestMapping)clazz.getAnnotation(RequestMapping.class);
            String classUrl = annotation2.value()[0];

            Permission parent = new Permission();

            parent.setName(annotation.name());
            parent.setDescs(annotation.desc());
            parent.setSn(clazz.getSimpleName());
            parent.setUrl(classUrl);
            parent.setParent(null);

            //入库
            permissionService.insert(parent);

            //否----获取带有权限注解的所有方法信息(Method)
            Method[] methods = clazz.getMethods();
            for (Method method : methods){
                RonghuaPermission annotation1 = method.getAnnotation(RonghuaPermission.class);
                //判断方法上权限注解是否为空
                if(Objects.isNull(annotation1)){
                    //是----说明无权限
                    continue;
                }

                //否----获取信息，封装对象

                Permission permission = new Permission();
                permission.setName(annotation1.name());
                permission.setDescs(annotation1.desc());
                permission.setSn(clazz.getSimpleName()+":"+method.getName());
                String methodUrl = getMethodUrl(method);
                permission.setUrl(classUrl+methodUrl);
                permission.setParent(parent);

                //入库
                permissionService.insert(permission);

            }
        }

    }

    public String getMethodUrl(Method method){
        String methodUrl = "";
        // 取枚举中定义的所有的请求类型
        RequestTypeEnum[] requestTypeEnums = RequestTypeEnum.values();
        for (RequestTypeEnum typeEnum : requestTypeEnums) {
            // 根据枚举中的不同的请求类型的class获取该类型对应的注解对象
            Annotation annotation = method.getAnnotation(typeEnum.getRequestType());
            // 如果该请求类型注解不存在就跳过
            if(Objects.isNull(annotation)){
                continue;
            }
            try {
                // 如果该请求类型注解存在,就获取它里面的value方法
                Class<? extends Annotation> aClass = annotation.annotationType();
                // aClass 是 对应的 请求类型的  字节码对象
                Method annotationMethod = aClass.getMethod("value");
                // 通过invoke调用该对象的value方法,获取结果
                // 反射执行 方法 方法名.invoke(对象)

                String[] value = (String[]) annotationMethod.invoke(annotation);
                // 如果value不为空且长度大于0,就赋值给methodUrl,并跳出循环
                if(value != null && value.length > 0){
                    methodUrl = value[0];
                    break;
                }
            }catch (Exception e) {
                e.printStackTrace();
                continue;
            }
        }
        return methodUrl;
    }
}
